package yuxi.com.kotlin

import android.app.Activity
import android.content.Intent
import android.content.Intent.ACTION_SEND
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import com.tencent.bugly.crashreport.CrashReport
import yuxi.com.kotlin.bugly.BuglyTag
import yuxi.com.kotlin.bugly.DataSaver

class MainActivity : Activity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //set tag
        CrashReport.setUserSceneTag(this, BuglyTag().TEST)
        //set data
        val result =DataSaver().putData("version",Build.VERSION.SDK_INT.toString())
        Log.i(MainActivity().javaClass.simpleName,"add data result:$result")
    }
    fun actionChooser(){
        val intent = Intent(ACTION_SEND)
        intent.type = "text/plain"
         intent.putExtra(Intent.EXTRA_TEXT,"VGJ.T加油！")
        intent.putExtra(Intent.EXTRA_PACKAGE_NAME,packageName)
        val result = packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL)
        if(result!=null&&!result.isEmpty()){
            if(result.size>1){
                var chooser = Intent.createChooser(intent, "分享")
                startActivityForResult(chooser,0)
            }
            else
                startActivityForResult(intent,0)
        }else{
            shareFailed("分享失败,未找到可分享的应用")
        }
    }
    fun shareClick(view :View){
        actionChooser()

        //crash()
    }
    fun crash(){
        CrashReport.testJavaCrash()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==0){
            if(resultCode== RESULT_OK){
                Toast.makeText(this,"分享成功",Toast.LENGTH_SHORT).show()
            }else{
                shareFailed("分享失败")
            }
        }
    }
   private fun shareFailed(  reason:String){
        Toast.makeText(this, reason,Toast.LENGTH_SHORT).show()
    }

}
